// src/stores/chat.js - 简化的聊天状态管理
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useChatStore = defineStore('chat', () => {
  // 核心状态
  const messages = ref([])
  const isStreaming = ref(false)
  const isRecording = ref(false)

  // 添加消息
  const addMessage = (content, type = 'user', audioUrl = null) => {
    const message = {
      id: `msg_${Date.now()}_${Math.random()}`,
      content,
      type,
      audioUrl,
      timestamp: new Date().toISOString()
    }
    messages.value.push(message)
    return message
  }

  // 更新消息
  const updateMessage = (messageId, updates) => {
    const index = messages.value.findIndex(m => m.id === messageId)
    if (index > -1) {
      messages.value[index] = { ...messages.value[index], ...updates }
    }
  }

  // 清空消息
  const clearMessages = () => {
    messages.value = []
  }

  // 设置消息列表
  const setMessages = (newMessages) => {
    messages.value = newMessages
  }

  // 设置流式状态
  const setStreaming = (value) => {
    isStreaming.value = value
  }

  // 设置录音状态
  const setRecording = (value) => {
    isRecording.value = value
  }

  return {
    messages,
    isStreaming,
    isRecording,
    addMessage,
    updateMessage,
    clearMessages,
    setMessages,
    setStreaming,
    setRecording
  }
})